package org.zpmis.api.business;

import org.zpmis.domain.dict.enums.SysDictEnum;
import org.zpmis.domain.dict.model.SysDict;
import org.zpmis.domain.dict.qo.DictListQo;
import org.zpmis.domain.dict.service.SysDictService;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.zpmis.domain.dict.vo.DictListVo;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created with CodeGenerator
 * Description:
 * @author  Yang
 * Date: 2021-5-31
 * Time: 13:57:41
 */
@Service
@Slf4j
public class SysDictWebService {
     @Reference
     private SysDictService sysDictService;

    /**
     * 查询对应业务的字典表
     * @param dictListQo
     * @return
     */
    public Map<String, Map<String, String>> dictList(DictListQo dictListQo) {
        String type = dictListQo.getType();
        List<String> names = Arrays.stream(type.split(",")).collect(Collectors.toList());
        Condition condition = new Condition(SysDict.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("isDeleted",SysDictEnum.NOT_DELETE.code());
        criteria.andEqualTo("businessType", SysDictEnum.SYSTEM_ADMIN.code());
        criteria.andIn("name", names);
        List<SysDict> resultList = sysDictService.findByCondition(condition);
        List<DictListVo> resultListVo = resultList.stream().map(DictListVo::new).collect(Collectors.toList());
        Map<String, Map<String, String>> resultMap = resultListVo.stream().collect(Collectors.groupingBy(dictListVo -> dictListVo.getName(), Collectors.toMap(DictListVo::getValue, DictListVo::getDescription, (v1, v2) -> v1)));

        return resultMap;
    }
}
